package com.zodiac.alioth.common.encrypt;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;

import java.security.MessageDigest;
import java.util.Random;

/**
 * MD5加密工具
 *
 * @author insight
 */
public class MD5Util {

    private static final String KEY_MD5 = "MD5";

    /**
     * 根据用户名和密码生成加密后的密码
     *
     * @param loginName 用户名
     * @param password  密码
     * @return md5Password
     */
    public static String generatePassword(String loginName, String password) {
        if (StringUtils.isBlank(loginName) || StringUtils.isBlank(password)) {
            return null;
        }
        String loginTemp = md5Hex(loginName);
        String passwordTemp = md5Hex(password);

        return generatePassword(loginTemp + passwordTemp);
    }

    /**
     * 校验密码是否正确
     * @param loginName     用户名
     * @param password      密码
     * @param md5Password   数据库中存的密码
     * @return true:验证通过；false:验证失败
     */
    public static boolean verifyPassword(String loginName, String password, String md5Password) {
        if (StringUtils.isBlank(loginName) || StringUtils.isBlank(password)) {
            return false;
        }
        String loginTemp = md5Hex(loginName);
        String passwordTemp = md5Hex(password);

        return verify(loginTemp + passwordTemp, md5Password);
    }

    /**
     * 生成含有随机盐的密码
     */
    private static String generatePassword(String password) {
        Random r = new Random();
        StringBuilder sb = new StringBuilder(16);
        sb.append(r.nextInt(99999999)).append(r.nextInt(99999999));
        int len = sb.length();
        if (len < 16) {
            for (int i = 0; i < 16 - len; i++) {
                sb.append("0");
            }
        }
        String salt = sb.toString();
        password = md5Hex(password + salt);
        char[] cs = new char[48];
        for (int i = 0; i < 48; i += 3) {
            cs[i] = password.charAt(i / 3 * 2);
            char c = salt.charAt(i / 3);
            cs[i + 1] = c;
            cs[i + 2] = password.charAt(i / 3 * 2 + 1);
        }
        return new String(cs);
    }


    /**
     * 校验密码是否正确
     */
    private static boolean verify(String password, String md5) {
        char[] cs1 = new char[32];
        char[] cs2 = new char[16];
        for (int i = 0; i < 48; i += 3) {
            cs1[i / 3 * 2] = md5.charAt(i);
            cs1[i / 3 * 2 + 1] = md5.charAt(i + 2);
            cs2[i / 3] = md5.charAt(i + 1);
        }
        String salt = new String(cs2);
        return md5Hex(password + salt).equals(new String(cs1));
    }

    /**
     * 获取十六进制字符串形式的MD5摘要
     */
    private static String md5Hex(String src) {
        try {
            MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
            byte[] bs = md5.digest(src.getBytes());
            return new String(new Hex().encode(bs));
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) {
        System.out.println(generatePassword("admin", "admin"));
        System.out.println(verifyPassword("admin", "admin", "a78f1e33b21a516075659908c2566646f486033a4a859b33"));
    }

}
